<template>
  <div class="container">
    <el-card>
      <!-- 面包屑导航 -->
      <el-breadcrumb
        separator-class="el-icon-arrow-right"
        style="border-bottom: 1px solid #ccc; padding-bottom: 15px"
      >
        <el-breadcrumb-item :to="{ path: '/subjects/list' }"
          ><span>学科管理</span></el-breadcrumb-item
        >
        <el-breadcrumb-item>大数据</el-breadcrumb-item>
        <el-breadcrumb-item>标签管理</el-breadcrumb-item>
      </el-breadcrumb>

      <el-row :gutter="20">
        <el-col :span="19">
          <!-- 标签、状态、新增标签 -->
          <el-form
            :inline="true"
            :v-model="filterForm"
            class="demo-form-inline"
          >
            <el-form-item label="标签名称">
              <el-input size="small" v-model="filterForm.tagName" placeholder="请输入标签名称"></el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-select  size="small" v-model="filterForm.state">
                <el-option v-for="s in stateSelect" :key='s.value' :label="s.label" :value="s.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button size="small" @click="filterForm.tagName='';filterForm.state=null">清除</el-button>
              <el-button type="primary" size="small" @click="filterTag"
                >查询</el-button
              >
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="5" style="padding-top: 5px">
          <el-link
            :underline="false"
            type="primary"
            style="font-size: 16px"
            @click="$router.push('/subjects/list')"
            ><i class="el-icon-back"></i>返回学科</el-link
          >
          <el-button @click="dialogFormVisible=true;title='新增标签'" type="success" size="small" style="margin-left: 10px"
            ><i class="el-icon-edit"></i> 新增标签</el-button
          >
        </el-col>
      </el-row>

      <!-- 数据提示 -->
      <el-alert type="info" show-icon :closable="false">
        数据共有{{ total }}条
      </el-alert>

      <!-- 表格显示 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column
          type="index"
          label="序号"
          width="120px"
          :index="indexMethod"
        >
        </el-table-column>
        <el-table-column prop="subjectName" label="所属学科"> </el-table-column>
        <el-table-column prop="tagName" label="标签名称"> </el-table-column>
        <el-table-column prop="username" label="创建者"> </el-table-column>
        <el-table-column prop="addDate" label="创建日期">
          <template #default="{ row }">{{
            row.addDate | timeFormater
          }}</template>
        </el-table-column>
        <el-table-column prop="state" label="状态">
          <template #default="{ row }">{{ row.state | stateFilter }}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template #default="{ row }">
            <el-button type="text" @click="banTag(row.id, row.state)">{{
              row.state === 0 ? 1 : 0 | stateFilter
            }}</el-button>
            <el-button type="text" :disabled='!row.state' @click="changeTage(row.id,row.subjectID,row.tagName)">修改</el-button>
            <el-button type="text" :disabled='!row.state' @click="delTag(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
      >
      </el-pagination>

      <!-- 新增标签 -->
      <el-dialog :title="title" :visible="dialogFormVisible" @close='close'>
        <el-form :model="newTags" :rules="rules" ref='tagForm' >
          <el-form-item label="所属学科" :label-width="formLabelWidth" prop='subjectID' v-show='title==="新增标签" && !$route.query.id'>
            <el-select v-model="newTags.subjectID" placeholder="请选择学科" style="width:80%">
              <el-option v-for="s in subjectList" :key='s.value' :label="s.label" :value="s.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="标签名称" :label-width="formLabelWidth" prop='tagName'>
            <el-input  style="width:80%" v-model="newTags.tagName" placeholder="请输入标签名称"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="close">取 消</el-button>
          <el-button type="primary" @click="goAddTag"
            >确 定</el-button
          >
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { status } from '@/api/hmmm/constants.js'
import { list, changeState, remove, add, update } from '@/api/hmmm/tags.js'
import { simple } from '@/api/hmmm/subjects.js'
import moment from 'moment'
export default {
  name: 'Tags',
  data () {
    return {
      formLabelWidth: '120px',
      stateSelect: status,
      filterForm: {
        tagName: '',
        state: null
      },
      tableData: [],
      rules: {
        tagName: [
          { required: true, message: '请输入新增标签名', trigger: 'blur' }
        ]
      },
      total: 0, // 标签总数
      page: 1,
      pages: null, // 总页数
      pagesize: 10,
      subjectList: [],
      subjectID: null,
      newTags: {
        subjectID: null,
        tagName: null
      },
      dialogFormVisible: false,
      title: ''
    }
  },
  filters: {
    stateFilter (val) {
      return status.find(i => i.value === val).label
    },
    timeFormater (val) {
      return moment(val).format('YYYY-MM-DD HH:mm:ss')
    }
  },
  created () {
    if (this.$route.query.id) {
      this.subjectID = this.$route.query.id
      this.newTags.subjectID = this.$route.query.id * 1
    }
    this.getTagsList()
    this.getSimpleSubjectList()
  },
  methods: {
    // 处理分页
    handleSizeChange (val) {
      this.pagesize = val
      this.getTagsList()
    },
    handleCurrentChange (val) {
      this.page = val
      this.getTagsList()
    },
    // 序号处理
    indexMethod (index) {
      return (index + 1) + (this.page - 1) * this.pagesize
    },
    // 筛选标签
    async filterTag () {
      const { data: { counts, items, pages } } = await list(this.filterForm)
      this.total = counts
      this.pages = pages
      this.tableData = items
    },
    // 获取标签列表,初始化页面数据
    async getTagsList () {
      const { data: { counts, items, pages } } = await list({ page: this.page, pagesize: this.pagesize, subjectID: this.subjectID })
      this.total = counts
      this.pages = pages
      this.tableData = items
    },
    // 初始化简单学科列表
    async getSimpleSubjectList () {
      const { data } = await simple()
      this.subjectList = data
    },
    // 处理操作
    async banTag (id, st) {
      await changeState({ id, state: st === 0 ? 1 : 0 })
      this.getTagsList()
    },
    async changeTage (id, subjectID, tagName) {
      this.title = '修改标签'
      this.dialogFormVisible = true
      this.newTags.subjectID = subjectID
      this.newTags.id = id
      this.newTags.tagName = tagName
    },
    async delTag (id) {
      const res = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch()
      console.log(res)
      if (res === 'confirm') {
        await remove({ id })
        if (this.tableData.length === 1 && this.page > 1) this.page--
        this.getTagsList()
      }
    },
    // 添加标签
    goAddTag () {
      this.$refs.tagForm.validate(async boo => {
        if (!boo) return
        if (this.newTags.id) { // id存在就是修改
          await update(this.newTags)
          this.$message.success('修改成功')
        } else {
          await add(this.newTags)
        }
        this.getTagsList()
        this.close()
      })
    },
    // 关闭Dialog
    close () {
      this.dialogFormVisible = false
      this.$refs.tagForm.resetFields()
    }
  }
}
</script>

<style scoped lang='scss'>
::v-deep .el-breadcrumb__inner span {
  color: #000 !important;
  font-weight: bold !important;
}
.el-row {
  margin-top: 15px;
}
.el-alert {
  height: 35px;
  line-height: 30px;
}
::v-deep .el-alert__icon.is-big {
  font-size: 16px !important;
  width: 20px;
  height: 13px;
}
/* 表格 */
.el-table {
  margin-top: 15px;
}
::v-deep .el-table[data-v-01d505a6] .el-table__header thead th {
  background-color: #e1e2d8d0 !important;
}
.el-pagination {
  float: right;
  margin: 20px 0;
}
</style>
